import  cv2
import numpy as np
if __name__ == '__main__':
    image_np = cv2.imread('work5.jpg')

    image_gray = cv2.cvtColor(image_np, cv2.COLOR_BGR2GRAY)

    # x_min_l, x_max_l = 345, 850
    # y_min_l, y_max_l = 585, 775
    # line_width = 2
    # cv2.rectangle(image_np, (x_min_l, y_min_l), (x_max_l, y_max_l), (0, 0, 255), line_width)
    _, image_thresh = cv2.threshold(image_gray[585:775, 345:850], 40, 255, cv2.THRESH_BINARY_INV)
    # image_roi = image_np.copy()[585:775, 345:850]


    countours, hierarchy = cv2.findContours(image_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in countours:
        # print(cnt.shape)
        x, y, z = map(int, np.random.randint(0, 256, 3))
        rect = cv2.minAreaRect(cnt)
        points = cv2.boxPoints(rect)

        points = np.int32(points)
        print(points)
        cv2.drawContours(image_np[585:775, 345:850], [points],-1, (x, y, z), 2 )


    cv2.imshow('', image_np)
    cv2.imwrite('zuoye5.jpg', image_np)
    cv2.waitKey(0)